<!-- 采购合同编辑 -->
<template>
  <div class="registerDetail">
    <van-nav-bar :title="$route.meta.title" left-text="返回" left-arrow :fixed="true" :placeholder="true" @click-left="$router.go(-1)" />
    <!-- 表单 -->
    <div class="formData">
      <van-form ref="form">
        <van-field v-model="formData.vinCode" name="车架号" label="车架号" input-align="right" placeholder="请输入" required :rules="[{ required: true }]" readonly />
        <van-field readonly v-model="formData.goodsTypeName" name="商品类型" label="商品类型" input-align="right" right-icon="arrow" placeholder="请选择" @click="handleSelect('goodsType', 'goodsTypeName')" :rules="[{ required: true }]" required />
        <van-field v-model="formData.salePrice" name="销售报价" label="销售报价" input-align="right" placeholder="请输入" required :rules="[{ required: true }]" />
        <van-field v-model="formData.carGuiPrice" name="新车指导价（含税）" label="新车指导价（含税）" input-align="right" placeholder="请输入" required :rules="[{ required: true }]" label-width="150px" />
        <!-- <van-field
          v-model="formData.showOrgName"
          name="车源地"
          label="车源地"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          readonly
          @click="handleSelect('showOrgId', 'showOrgName')"
          required
        />

        <van-field
          v-model="formData.firstDate"
          name="首次上牌日期"
          label="首次上牌日期"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleSelectTime('firstDate')"
          readonly
          required
        />
        <van-field
          v-model="formData.carModel"
          name="车型"
          label="车型"
          input-align="right"
          placeholder="请输入"
          required
        />
        <van-field
          v-model="formData.carColor"
          name="车身颜色"
          label="车身颜色"
          input-align="right"
          placeholder="请输入"
          required
        />
        <van-field
          v-model="formData.carMile"
          name="公里数"
          label="公里数(km)"
          input-align="right"
          placeholder="请输入"
          required
        />
        <van-field
          required
          v-model="formData.shDisplacement"
          name="排量"
          label="排量(L/T)"
          input-align="right"
          placeholder="请输入"
        />-->

        <!-- 下拉选择弹窗 -->
        <van-popup v-model="showSelectPicker" position="bottom">
          <van-picker show-toolbar :columns="columns" @confirm="handleSelectConfirm" @cancel="showSelectPicker = false" value-key="name" />
        </van-popup>

        <!-- 时间选择弹窗 -->
        <van-popup v-model="showTimerPicker" position="bottom">
          <van-datetime-picker v-model="currentDate" type="date" @confirm="onTimeConfirm($event)" @cancel="showTimerPicker = false" :formatter="formatter" />
        </van-popup>
        <div class="footerBtn">
          <div class="submitBtn" @click="formOnSubmit()">提交</div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { Field, Form, DatetimePicker, Notify } from "vant";
import { getCookies } from "@/utils/util.js";
import sellManageApi from "@/api/sellManageApi";
import commonApi from "@/api/commonApi";
export default {
  name: "PaymentOrderEdit",
  components: {
    Field,
    DatetimePicker,
    Form,
    Notify
  },
  data() {
    return {
      showTimerPicker: false,
      formData: {
        goodsType: "1", //商品类型，必填，String
        goodsTypeName: "二手车", //商品类型，必填，String
        // showOrgId: '', //车源地，必填，String
        vinCode: "", //车架号,
        carId: "",
        // firstDate: '', //首次上牌日期,必填，String
        // carModel: '', //车型,必填，String
        // carColor: '', //车身颜色,必填，String
        // carMile: '', //公里数,必填，String
        // shDisplacement: '', //排量，提交必填，String
        carGuiPrice: "",
        salePrice: ""
      },

      columns: [],
      showSelectPicker: false,
      detailId: "",
      userId: getCookies("userId"), //用户ID，必填，String

      currentDate: new Date(),
      showOrgList: [],
      goodsTypeList: []
    };
  },

  mounted() {
    this.getGoodsType();
    this.getShowOrgByUser();
    if (this.$route.query) {
      this.formData.vinCode = this.$route.query.vinCode;
    }
    if (this.$route.query) {
      this.formData.carId = this.$route.query.carId;
    }
  },
  methods: {
    // 获取展厅
    getShowOrgByUser() {
      sellManageApi
        .secondHandCarFacade({
          userId: this.userId,
          orgKey: "showOrg", // orgkey
          method: "getShowOrgByUser"
        })
        .then(res => {
          if (res.data) {
            this.showOrgList = res.data;
          }
        });
    },

    // 商品类型
    getGoodsType() {
      commonApi
        .getEnums({
          key: "goodsType",
          userId: this.userId
        })
        .then(res => {
          if (res.data) {
            let data = [];
            res.data.forEach((item, index) => {
              if (item.name) {
                data.push(item);
              }
            });
            this.goodsTypeList = data;
          }
        });
    },

    // 选择下拉
    handleSelect(field, fieldName) {
      this.showSelectPicker = true;
      if (field == "goodsType") {
        this.columns = this.goodsTypeList;
      } else if (field == "showOrgId") {
        this.columns = this.showOrgList;
      }
      this.field = field;
      this.fieldName = fieldName;
    },
    // 选择下拉确认
    handleSelectConfirm(value, index) {
      if (this.field == "showOrgId") {
        this.formData[this.field] = value.billId;
        this.formData[this.fieldName] = value.name;
      } else {
        this.formData[this.field] = value.value;
        this.formData[this.fieldName] = value.name;
      }

      this.showSelectPicker = false;
    },

    onConfirm(val, field) {
      let year = val.getFullYear();
      let month = val.getMonth() + 1;
      let day = val.getDate();
      let hour = val.getHours();
      let minute = val.getMinutes();
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`;
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`;
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[field] = `${year}-${month}-${day}`;
      this.showTimerPicker = false;
    },

    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      } else if (type === "month") {
        return `${value}月`;
      } else if (type === "day") {
        return `${value}日`;
      } else if (type === "hour") {
        return `${value}时`;
      } else if (type === "minute") {
        return `${value}分`;
      } else if (type === "second") {
        return `${value}秒`;
      }
      return value;
    },
    // 时间选择
    handleSelectTime(field) {
      this.timeField = field;
      this.showTimerPicker = true;
    },
    onTimeConfirm(val) {
      let year = val.getFullYear();
      let month = val.getMonth() + 1;
      let day = val.getDate();
      let hour = val.getHours();
      let minute = val.getMinutes();
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`;
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`;
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[this.timeField] = `${year}-${month}-${day} `;
      this.showTimerPicker = false;
    },

    formOnSubmit(type) {
      this.$refs.form
        .validate()
        .then(() => {
          sellManageApi
            .secondHandCarFacade({
              ...this.formData,
              method: "addStockData"
            })
            .then(res => {
              console.log(res);
              if (res.code == 1) {
                Notify({ type: "success", message: "提交成功" });
                this.$router.go(-1);
              }
            });
          this.loading = true;
        })
        .catch(() => {
          this.loading = false;
          this.$toast.fail("请完善信息");
        });
    }
  }
};
</script>

<style lang="less" scoped>
body {
  background: #f3f3f3;
}
.flex {
  display: flex;
  align-items: center;
}
.registerDetail {
  overflow: hidden;
  .detail_header {
    justify-content: space-between;
    margin: 10px 10px;
    border-radius: 5px;
    background: #fff;
    padding: 15px;
    .detail_header_lt {
      .carNo {
        font-size: 16px;
        font-weight: bold;
        margin-bottom: 10px;
        .carStatus {
          border: 1px solid #35bc04;
          color: #35bc04;
          border-radius: 5px;
          padding: 5px 5px;
          font-size: 12px;
          text-align: center;
          margin-left: 5px;
        }
      }
      .hank {
        font-size: 14px;
        color: #666;
        .name {
          border-right: 2px solid #ccc;
          padding-right: 5px;
          margin-right: 5px;
        }
      }
    }
    .detail_header_rt {
      justify-content: flex-start;
      .txt {
        font-size: 14px;
        color: #161616;
      }
      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 16px;
        padding: 5px;
        border-radius: 50%;
        margin-right: 5px;
      }
    }
  }
  .formData {
    background: #fff;
    overflow: hidden;
  }
}

.footerBtn {
  display: flex;
  justify-content: space-between;
  width: 80%;
  margin: auto;
  padding-bottom: 50px;
  background: #fff;
  margin-top: 25px;
  .saveBtn {
    background: #ccc;
    color: #fff;
    width: 45%;
    margin: auto;
    text-align: center;
    padding: 12px;
    border-radius: 5px;
    font-size: 15px;
    margin-right: 10px;
  }
  .submitBtn {
    background: #409eff;
    color: #fff;
    width: 100%;
    margin: auto;
    text-align: center;
    padding: 12px;
    border-radius: 5px;
    font-size: 15px;
  }
}
.car_title {
  padding: 10px;
  font-size: 15px;
  font-weight: bold;
  color: #333;
}
</style>
